//
//  GKNovelSetManager.swift
//  MySwiftObject
//
//  Created by wangws1990 on 2019/9/12.
//  Copyright © 2019 wangws1990. All rights reserved.
//

import UIKit
let novelManager = GKNovelSetManager();

class GKNovelSetManager: NSObject {
    lazy var model: GKNovelSet = {
        return GKNovelSetManager.getModel();
    }()
    class var manager : GKNovelSetManager{
        return novelManager;
    }
    class func setNight(night:Bool){
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        if model.night != night {
            model.night = night;
            GKNovelSetManager.saveModel(model: model);
        }
    }
    class func setLandscape(landscape:Bool){
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        if model.landscape != landscape {
            model.landscape = landscape;
            GKNovelSetManager.saveModel(model: model);
        }
    }
    class func setTraditiona(traditiona:Bool){
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        if model.traditiona != traditiona {
            model.traditiona = traditiona;
            GKNovelSetManager.saveModel(model: model);
        }
    }
    class func setBrightness(brightness:Float){
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        if model.brightness != brightness {
            model.brightness = brightness;
            GKNovelSetManager.saveModel(model: model);
        }
    }
    class func setFontName(fontName:String){
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        if model.fontName != fontName {
            model.fontName = fontName;
            GKNovelSetManager.saveModel(model: model);
        }
    }
    class func setFont(font:Float){
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        if model.font != font {
            model.font = font;
            GKNovelSetManager.saveModel(model: model);
        }
    }
    class func setSkin(skin:GKNovelState){
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        if model.skin != skin {
            model.skin = skin;
            GKNovelSetManager.saveModel(model: model);
        }
    }
    class func setBrowse(browse:GKNovelBrowse){
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        if model.browse != browse {
            model.browse = browse;
            GKNovelSetManager.saveModel(model: model);
        }
    }
    class func saveModel(model:GKNovelSet){
        let defaults :UserDefaults = UserDefaults.standard;
        let data : NSData = NSKeyedArchiver.archivedData(withRootObject: model) as NSData;
        if data.count > 0 {
            defaults.set(data, forKey:GKSetInfo)
            defaults.synchronize();
        }
    }
    class func getModel() -> GKNovelSet {
        let data = UserDefaults.standard.object(forKey:GKSetInfo)
        var model:GKNovelSet!
        if data != nil {
            model =  NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as? GKNovelSet
        }else{
            model = GKNovelSetManager.createModel();
        }
        return model;
    }
    
    class func createModel() -> GKNovelSet{
        let model:GKNovelSet = GKNovelSet();
        model.font = 20;
        model.color = "333333";
        model.fontName = "PingFang-SC-Regular";
        model.lineSpacing = 10;
        model.firstLineHeadIndent = 25 ;
        model.paragraphSpacingBefore = 10;
        model.paragraphSpacing = 10;
        model.skin = .defaults;
        model.browse = .defaults;
        model.night = false
        model.landscape = false;
        model.traditiona = false;
        return model;
    }
    class func defaultFont()-> NSDictionary{
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        let paragraphStyle : NSMutableParagraphStyle = NSMutableParagraphStyle.init();
        paragraphStyle.lineSpacing = CGFloat(model.lineSpacing ?? 10);//段落 行间距
        paragraphStyle.firstLineHeadIndent  = CGFloat(model.firstLineHeadIndent ?? 20);//首行缩进
        paragraphStyle.paragraphSpacingBefore = CGFloat(model.paragraphSpacingBefore ?? 10); //段间距，当前段落和上个段落之间的距离。
        paragraphStyle.paragraphSpacing = CGFloat(model.paragraphSpacing ?? 10); //段间距，当前段落和下个段落之间的距离。
        paragraphStyle.alignment = .justified;//两边对齐
        paragraphStyle.allowsDefaultTighteningForTruncation = true;
        let dic :NSMutableDictionary = NSMutableDictionary.init();
        let font : UIFont = UIFont.init(name: model.fontName, size:CGFloat(model.font))!;
        let color : UIColor = (model.skin == .caffee) ? UIColor.init(hex: "dddddd") : UIColor.init(hex:model.color ?? "666666");
        dic.setObject(color, forKey: NSAttributedString.Key.foregroundColor.rawValue as NSCopying);
        dic.setObject(paragraphStyle, forKey: NSAttributedString.Key.paragraphStyle.rawValue as NSCopying)
        dic.setObject(font, forKey: NSAttributedString.Key.font.rawValue as NSCopying)
        return dic
    }
    class func defaultSkin() ->UIImage{
        let model : GKNovelSet = GKNovelSetManager.manager.model;
        if model.night {
            return UIImage.init(named: "icon_read_black")!;
        }
        let index : Int = model.skin!.rawValue;
        let listData : [GKNovelSkin] = GKNovelSetManager.defaultSkinDatas();
        let modelName :GKNovelSkin = listData[index];
        return UIImage.init(named: modelName.skin)!;
    }
    class func defaultSkinDatas() -> [GKNovelSkin]{
        let model1 : GKNovelSkin =  GKNovelSkin.vcWithModel(title: "", skin: "icon_read_yellow", state: .defaults);
        let model2 : GKNovelSkin =  GKNovelSkin.vcWithModel(title: "", skin: "icon_read_green", state: .green);
        let model3 : GKNovelSkin =  GKNovelSkin.vcWithModel(title: "", skin: "icon_read_coffee", state: .caffee);
        let model4 : GKNovelSkin =  GKNovelSkin.vcWithModel(title: "", skin: "icon_read_fenweak", state: .pink);
        let model5 : GKNovelSkin =  GKNovelSkin.vcWithModel(title: "", skin: "icon_read_fen", state: .fen);
        let model6 : GKNovelSkin =  GKNovelSkin.vcWithModel(title: "", skin: "icon_read_zi", state: .zi);
        return [model1,model2,model3,model4,model5,model6];
    }
}
